package com.zjw.zy.utils;

import androidx.annotation.NonNull;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/26 17:29
 */
public final class GsonUtils {

    private static final Gson sGson = new Gson();


    public static String toJson(Object object) {
        return sGson.toJson(object);
    }


    public static <T> T fromJson(@NonNull String json, @NonNull Class<T> clazz) {
        return sGson.fromJson(json, clazz);
    }

    public static <T> T fromJson(@NonNull String json, @NonNull Type type) {
        return sGson.fromJson(json, type);
    }


    public static String getNode(@NonNull String json, @NonNull String node) {
        try {
            JSONObject jsonObject = new JSONObject(json);

            return jsonObject.getString(node);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }
}
